package top.gbiot.data.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.gbiot.common.annotation.Log;
import top.gbiot.common.core.controller.BaseController;
import top.gbiot.common.core.domain.AjaxResult;
import top.gbiot.common.core.page.TableDataInfo;
import top.gbiot.common.enums.BusinessType;
import top.gbiot.common.utils.poi.ExcelUtil;
import top.gbiot.data.domain.IotData;
import top.gbiot.data.service.IIotDataService;


/**
 * 设备数据Controller
 * 
 * @author gb
 * @date 2021-02-22
 */
@Controller
@RequestMapping("/iot/data")
public class IotDataController extends BaseController
{
    private String prefix = "iot/data";

    @Autowired
    private IIotDataService iotDataService;

    @RequiresPermissions("iot:data:view")
    @GetMapping()
    public String data()
    {
        return prefix + "/data";
    }

    /**
     * 查询设备数据列表
     */
    @RequiresPermissions("iot:data:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IotData iotData)
    {
        startPage();
        List<IotData> list = iotDataService.selectIotDataList(iotData);
        return getDataTable(list);
    }

    /**
     * 导出设备数据列表
     */
    @RequiresPermissions("iot:data:export")
    @Log(title = "设备数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IotData iotData)
    {
        List<IotData> list = iotDataService.selectIotDataList(iotData);
        ExcelUtil<IotData> util = new ExcelUtil<IotData>(IotData.class);
        return util.exportExcel(list, "data");
    }

    /**
     * 新增设备数据
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备数据
     */
    @RequiresPermissions("iot:data:add")
    @Log(title = "设备数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IotData iotData)
    {
        return toAjax(iotDataService.insertIotData(iotData));
    }

    /**
     * 修改设备数据
     */
    @GetMapping("/edit/{dataId}")
    public String edit(@PathVariable("dataId") String dataId, ModelMap mmap)
    {
        IotData iotData = iotDataService.selectIotDataById(dataId);
        mmap.put("iotData", iotData);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备数据
     */
    @RequiresPermissions("iot:data:edit")
    @Log(title = "设备数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IotData iotData)
    {
        return toAjax(iotDataService.updateIotData(iotData));
    }

    /**
     * 删除设备数据
     */
    @RequiresPermissions("iot:data:remove")
    @Log(title = "设备数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(iotDataService.deleteIotDataByIds(ids));
    }
}
